﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Collections;
using System.Threading;
using System.Windows.Forms;

namespace MessMain
{
    public class Client
    {
        private NetworkStream myNetStream;
        private TcpClient myClient;
        private BlockingQueue<string> list;
        Comunication c;

        private string ip;
        private int port;
        private string limba;

        public Client(string ip, int port, Comunication cc, string limba)
        {
            this.ip = ip;
            this.port = port;
            this.limba = limba;
            c = cc;
        }

        public void init()
        {
            myClient = new TcpClient();
            try
            {
                myClient.Connect(this.ip, this.port);
                myNetStream = myClient.GetStream();
            }
            catch (System.Net.Sockets.SocketException)
            {
                myClient = null;
                Console.WriteLine("Server nediponibil!");

                XMLParser parser = new XMLParser(@"C:\Language" + limba + ".xml");

                Dictionary<string, string> dict =
                    parser.ExtractAttributePairs("MesajeEroare", string.Empty, string.Empty, "name", "formula");

                MessageBox.Show(dict["ErrorConectareServer"] + ip,
                                dict["TitluEroare"],
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
            finally
            {
                list = new BlockingQueue<string>();
            }
        }
        public void run()
        {
            ReadMsgSock rms = new ReadMsgSock(myClient, c, limba);
            Thread th = new Thread(new ThreadStart(rms.run));
            th.Start();
            try
            {
                while (myClient != null &&
                       myClient.Connected)
                {
                    string message = getMsg();
                    if (message.Trim().CompareTo("")!=0)
                    {
                        Byte[] sendBytes = Encoding.ASCII.GetBytes(message);
                        myNetStream.Write(sendBytes, 0, sendBytes.Length);
                        Thread.Sleep(100);
                    }
                }
                Console.WriteLine("ServerDeconectat2");
                if (myNetStream != null)
                    myNetStream.Close();
                if (myClient != null)
                    myClient.Close();
            }
            catch (System.ObjectDisposedException)
            {
                Console.WriteLine("ServerDeconectat5");
                myNetStream.Close();
                myClient.Close();
            }
            catch (System.IO.IOException)
            {
                Console.WriteLine("ServerDeconectat4");
                myNetStream.Close();
                myClient.Close();
            }

        }

        public void putMsg(string s)
        {
            list.addString(s);
        }

        private string getMsg()
        {
            return list.getString();
        }
    }
}
